﻿using System.Collections.Generic;
using System.Linq;
using QFramework;
using UnityEngine;

namespace ProjectMyFarmAndShe
{
    public interface IEmailSystem : ISystem
    {
        List<EmailData> EmailDatas { get; }
        void AddEmail(EmailData data);
    }
    
    public class EmailSystem : AbstractSystem,IEmailSystem
    {
        public List<EmailData> EmailDatas { get; private set; }
        public List<EmailData> EmailDatasQueue = new List<EmailData>();
        public static EasyEvent OnAddEmail = new EasyEvent();
        
        protected override void OnInit()
        {
            EmailDatas = new List<EmailData>();

            var saveUtility = this.GetUtility<ISaveUtility>();
            
            ActionKit.OnUpdate.Register(() =>
            {
                if (EmailDatasQueue.Count == 0) return;
                foreach (var emailData in EmailDatasQueue.Where(data=>data.EmailSendDay == Global.Day.Value).ToList())
                {
                    EmailDatas.Add(emailData);
                    OnAddEmail.Trigger();
                    EmailDatasQueue.Remove(emailData);
                }
            });

            var emailDatas = saveUtility.LoadString("EmailDatas", string.Empty);
            if (string.IsNullOrEmpty(emailDatas))
            {
                
            }
            else
            {
                var saveEmailData = JsonUtility.FromJson<SaveEmailData>(emailDatas);
                foreach (var saveEmailConfig in saveEmailData.Datas)
                {
                    EmailDatas.Add(new EmailData()
                        .WithTitle(saveEmailConfig.Title)
                        .WithPublisher(saveEmailConfig.Publisher)
                        .WithInfo(saveEmailConfig.Info)
                        .WithSendDay(saveEmailConfig.EmailSendDay)
                        .WithRead(saveEmailConfig.IsRead));
                }
            }

            var emailQueueData = saveUtility.LoadString("EmailQueueDatas", string.Empty);
            if (string.IsNullOrEmpty(emailQueueData))
            {
                
            }
            else
            {
                var saveEmailQueueData = JsonUtility.FromJson<SaveEmailData>(emailQueueData);
                foreach (var saveEmailConfig in saveEmailQueueData.Datas)
                {
                    EmailDatasQueue.Add(new EmailData()
                        .WithTitle(saveEmailConfig.Title)
                        .WithPublisher(saveEmailConfig.Publisher)
                        .WithInfo(saveEmailConfig.Info)
                        .WithSendDay(saveEmailConfig.EmailSendDay)
                        .WithRead(saveEmailConfig.IsRead));
                }
            }
            
            saveUtility.AddSaveDataAction((system) =>
            {
                var saveEmailData = new SaveEmailData();
                foreach (var emailData in EmailDatas)
                {
                    saveEmailData.Datas.Add(new SaveEmailConfig()
                    {
                        Title = emailData.Title,
                        Publisher = emailData.Publisher,
                        Info = emailData.Info,
                        EmailSendDay = emailData.EmailSendDay,
                        IsRead = emailData.IsRead.Value
                    });
                }

                var saveEmailQueueData = new SaveEmailData();
                foreach (var emailData in EmailDatasQueue)
                {
                    saveEmailQueueData.Datas.Add(new SaveEmailConfig()
                    {
                        Title = emailData.Title,
                        Publisher = emailData.Publisher,
                        Info = emailData.Info,
                        EmailSendDay = emailData.EmailSendDay,
                        IsRead = emailData.IsRead.Value
                    });
                }
                
                system.SaveString("EmailDatas",JsonUtility.ToJson(saveEmailData));
                system.SaveString("EmailQueueDatas",JsonUtility.ToJson(saveEmailQueueData));
            });
        }
        
        public void AddEmail(EmailData data)
        {
            EmailDatasQueue.Add(data);
        }
        
    }
}